var qrtzJobModule = angular.module('qrtzJobModule', ['paginationService', 'anajaxService','agularValidate','innerPaginationService']);

qrtzJobModule.controller('QrtzJobController', function($scope, $http,pagination,anajax,innerPagination) {
	$scope.jobForm={};
	$scope.operatelabel="";
	$scope.jobGroupName={}
	$scope.jobQueryForm={};
	$scope.jobQueryForm.jobGroup="-1";
	$scope.jobQueryForm.triggerState="-1";
	$scope.params=[{"param_name":"","param_value":"","operate":"add","operateValue":"添加"}];
	$scope.bean ={};
	$scope.tableOperate="";
	
	var init=function(){
		
		 $(".form_datetime").datetimepicker({format: 'yyyy-mm-dd hh:ii'});
	}
	
	init();
	/**
	 * 查询
	 */
	var query = function(params) {
		if($scope.jobQueryForm.jobGroup=="-1"){
			$scope.jobQueryForm.jobGroup=null;
		}
		if($scope.jobQueryForm.triggerState=="-1"){
			$scope.jobQueryForm.triggerState=null;
		}
		var url = commonutil.actionPath + '/qrtz/job/joblist';
		anajax.doajax(url, params, function(data) {
			if($scope.jobQueryForm.jobGroup==null){
				$scope.jobQueryForm.jobGroup="-1";
			}
			if($scope.jobQueryForm.triggerState==null){
				$scope.jobQueryForm.triggerState="-1";
			}
			$scope.resultList = data.resultList;
			pagination.setData(data.pagination);
			$scope.pagination = data.pagination; 
		});
	};
	
	/**
	 * 查询
	 */
	var queryJobGroup = function() {
		var url = commonutil.actionPath + '/qrtz/job/grouplist';
		anajax.doajax(url, null, function(data) {
			$scope.jobGroupName=data;
		});
	};
	queryJobGroup();
	query($scope.jobQueryForm); 
	
	/**
	 * 点击查询
	 */
	$scope.doquery = function() {
		
		query($scope.jobQueryForm);
	};
	
	/**初始化新增*/
	$scope.goAdd = function(index) {
		$scope.jobForm = {};
		$scope.stations = [];
		$scope.rightPart = 'add';
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('新增任务');
	};
	
	
	$scope.doCancel = function(){
		$scope.jobForm = {};
		pageutil.hideRightBar();
	}
	
	$scope.add_param_div=function (index){
		if(index==0){
			var tr={"param_name":"","param_value":"","operate":"add","operateValue":"移除"};
			$scope.params.push(tr);
		}else{
			$scope.params.splice(index, 1);
		}
	}
	
	
	/**
	 * 执行添加
	 */
	$scope.doAdd = function() {
		var res = Validator.validate($("#edit-data-warp"));
		if(!res){
			return;
		}
		if($scope.rightPart=='add'){
			$scope.add($scope.jobForm);
		}else{
			$scope.modify($scope.jobForm);
		}
		
	};
	
	
	/**
	 * 执行添加
	 */
	$scope.add = function(postData) {
		var names=$("[name=params_name]");
		var params_name=[];
		$(names).each(function(i, element) {
			params_name.push(element.value);
		});
		var values=$("[name=params_value]");
		var params_value=[];
		$(values).each(function(i, element) {
			params_value.push(element.value);
		});
		
		var url = commonutil.actionPath + '/qrtz/job/formadd?params_name='+params_name+'&params_value='+params_value;
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	

	/**
	 * 弹出确认启动任务窗口
	 */
	$scope.showStartJobWin = function($index){
		var assetBean = $scope.menuResultList[$index];
		$scope.bean= assetBean;
		$scope.tableOperate="startjob";
		$scope.operatelabel="确定启动"+$scope.delMenuBean.jobName+"任务吗？";
		$("#confirm_del_menu_asset_div").modal('show');
	};
	
	/**
	 * 弹出确认暂停任务窗口
	 */
	$scope.showBreakJobWin = function($index){
		var assetBean = $scope.menuResultList[$index];
		$scope.tableOperate="breakjob";
		$scope.bean= assetBean;
		$scope.operatelabel="确定暂停"+$scope.delMenuBean.jobName+"任务吗？";
		$("#confirm_del_menu_asset_div").modal('show');
	};
	
	/**
	 * 弹出确认删除任务窗口
	 */
	$scope.showDeleteWin = function($index){
		var assetBean = $scope.menuResultList[$index];
		$scope.tableOperate="delete";
		$scope.bean= assetBean;
		$scope.operatelabel="确定删除"+$scope.delMenuBean.jobName+"任务吗？";
		$("#confirm_del_menu_asset_div").modal('show');
	};
	
	/**
	 * 弹出确认立即执行任务窗口
	 */
	$scope.showExecJobWin = function($index){
		var assetBean = $scope.menuResultList[$index];
		$scope.tableOperate="execjob";
		$scope.bean= assetBean;
		$scope.operatelabel="确定立即执行"+$scope.delMenuBean.jobName+"任务吗？";
		$("#confirm_del_menu_asset_div").modal('show');
	};
	
	
	/**
	 * 任务操作
	 */
	$scope.doAdd = function() {
		
		if($scope.tableOperate=='startjob'){
			
			$scope.resumeJob();
		}else if($scope.tableOperate=='breakjob'){
			$scope.pauseJob();
		}else if($scope.tableOperate=='delete'){
			$scope.delJob();
		}else if($scope.tableOperate=='execjob'){
			$scope.execJob();
		}
	};
	
	
	
	//删除任务
//	delJob()
	$scope.delJob = function() {
	    console.log($scope.delBean);
		var url = commonutil.actionPath + '/qrtz/job/delete';
		var names=$scope.bean.jobName+"-"+$scope.bean.triggerGroup;
		anajax.doajax(url,{jobNames:names}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('删除成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	//启动任务
//	resumeJob
	$scope.resumeJob = function() {
	    console.log($scope.delBean);
		var url = commonutil.actionPath + '/qrtz/job/resume';
		var names=$scope.bean.jobName+"-"+$scope.bean.triggerGroup;
		anajax.doajax(url,{jobNames:names}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('启动成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	//暂停任务
//	pauseJob
	
	$scope.pauseJob = function() {
	    console.log($scope.delBean);
		var url = commonutil.actionPath + '/qrtz/job/pause';
		var names=$scope.bean.jobName+"-"+$scope.bean.triggerGroup;
		anajax.doajax(url,{jobNames:names}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('暂停成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	//立即执行任务
//	execJob
	
	$scope.execJob = function() {
	    console.log($scope.delBean);
		var url = commonutil.actionPath + '/qrtz/job/execute';
		var names=$scope.bean.jobName+"-"+$scope.bean.triggerGroup;
		anajax.doajax(url,{jobNames:names}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('启动成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
});